<?php

class editor extends content
{
	var $literary;
	var $edit;
	var $edit_image;
	var $edit_meta;
	var $all_post;
	
	function editor()
	{
		global $conf;
		mp('initializing editor');
		$this->display=true;
		$conf->page_render='assets/pages/index-editor.php';	
	}
	
	function get_literary_content($ptype)
	{
		unset($this->where);
		$this->where['post_type']=$ptype;
		$this->get_content();
		$this->literary = $this->data;		
		
		
	}
	function get_all_post()
	{
		unset($this->where);
		$this->get_content(1);
		$this->all_post = $this->data;	
	}
	function get_content($mode)
	{
		if($mode!=1)		
			$where=process_input($this->where);
		else 
		{
			$w[0]='post_type';
			$w[1]='';
			$w[2]='!=';
			$where[] = $w;
     			
		} 

			$ij[0]=TRUE;
			$ij[1]='content';
			$ij[2][0]='*';						
			$ij[3][0]='id';
			$ij[3][1]='id';
				
		
		mp('getting content');
		$this->data=get_sql('content_post','id',$where,$this->options,$ij);
	}
	function get_content_image()
	{
		$where=process_input($this->where);
		return get_sql('content_image','id',$where,$this->options,$ij);
	}
	function get_content_meta()
	{
		$where=process_input($this->where);
		return get_sql('content_meta','id',$where,$this->options,$ij);
	}
	function get_authors()
	{
		
		global $conf;
		$category=new content_category();
		unset($category->where);
		$category->where['parent']=18;
		$category->where['type']='post';
		$category->get_category();
		return $category->data;
	}
	function get_edit_content($id)
	{
		unset($this->where);
		$this->where['id']=$id;
		$this->get_content();
		$this->edit = current($this->data);		
		$this->edit_image = current($this->get_content_image());
		$this->edit_meta = current($this->get_content_meta());
	}
	function generate_h1($mode)
	{
		global $conf;
				
		$c = new content;
		switch($mode)
		{   
			case 1: 	
						$count = (int)$conf->a['headline-count-home'];
						$h = $c->get_h1(0,$count);  
						$table_title = "home-headline";
						$column = 'h1';
						$class = "h1-edit-select";
						break;
			
			case 2: 	$count = (int)$conf->a['headline-count-digradio'];
						$h = $c->get_h1(18,$count); 
						$column = 'd1';	
						$table_title = "digradio-headline";
						$class = "d1-edit-select"; 
						break;
		}	
		   
		echo "<table class='edit-table'>";
		echo "<tr><th colspan=2>$table_title</th></tr>";	   
	   
		
		$ctr=1;
		foreach($h as $x)
		{
			$str =  'h1'.$ctr;
			echo "<tr><td id=".$ctr.",".$x['id'].">".$x['title'];
			echo "</td>";
	   	
			echo "<td><select class='edit-selection'>";
				foreach($this->all_post as $y)
				{
					echo "<option value=".$y[$column].",".$y['id'].">".$y['title']."</option>";   	
				}
			echo "</select>";
			echo "<span  class='$class edit-select'>select</span>";
			echo "</td></tr>";

			$ctr++;
		}
	if($count>1) 
		echo "<tr><td colspan=2 class='remove-h1 edit-pointer' id=".$table_title."-".$count.">REMOVE</td>";		  
	echo "<tr><td colspan=2 class='add-h1 edit-pointer' id=".$table_title."-".$count.">ADD</td>";
	  
	echo "</table>";
	}
	function generate_posts()
	{
		$c = new content;
		global $conf;
		$h = $c->get_posts(); 
		$column = 'w';	
		//make news table---------------------------------------------
		
		$table_title = "digradio-news";
		$class = "n-edit-select";
		echo "<table class='edit-table'>";
		echo "<tr><th colspan=2>$table_title</th></tr>";	   
	   $news_count = (int)$conf->a['news-count-digradio'];
		$ctr=1;
		foreach($h as $x)
		{
			if($x['post_type']=='news' && $ctr<=$news_count){
				$str =  'h1'.$ctr;
				echo "<tr><td id=".$ctr.",".$x['id'].">".$x['title'];
				echo "</td>";
		   	
				echo "<td><select class='edit-selection'>";
					foreach($this->all_post as $y)
					{
						if($y['post_type']=='news')
							echo "<option value=".$y[$column].",".$y['id'].">".$y['title']."</option>";   	
					}
				echo "</select>";
				echo "<span  class='$class edit-select'>select</span>";
				echo "</td></tr>";

			$ctr++;
			}
		}
	if($news_count>1) 
		echo "<tr><td colspan=2 class='remove-h1 edit-pointer' id=".$table_title."-".$article_count.">REMOVE</td>";		  
	echo "<tr><td colspan=2 class='add-h1 edit-pointer' id=".$table_title."-".$news_count.">ADD</td>";
	echo "</table>";
	//end table------------------------------------------------------------------
		//make article table---------------------------------------------
		
		$table_title = "digradio-articles";
		$class = "a-edit-select";
		echo "<table class='edit-table'>";
		echo "<tr><th colspan=2>$table_title</th></tr>";	   
	   $article_count = (int)$conf->a['articles-count-digradio'];
		$ctr=1;
		foreach($h as $x)
		{
			if($x['post_type']=='article' && $ctr<=$article_count){
				$str =  'h1'.$ctr;
				echo "<tr><td id=".$ctr.",".$x['id'].">".$x['title'];
				echo "</td>";
		   	
				echo "<td><select class='edit-selection'>";
					foreach($this->all_post as $y)
					{
						if($y['post_type']=='article')
							echo "<option value=".$y[$column].",".$y['id'].">".$y['title']."</option>";   	
					}
				echo "</select>";
				echo "<span  class='$class edit-select'>select</span>";
				echo "</td></tr>";

			$ctr++;
			}
		}
	if($article_count>1) 
		echo "<tr><td colspan=2 class='remove-h1 edit-pointer' id=".$table_title."-".$article_count.">REMOVE</td>";		  
	echo "<tr><td colspan=2 class='add-h1 edit-pointer' id=".$table_title."-".$article_count.">ADD</td>";
	echo "</table>";
	//end table------------------------------------------------------------------
	//make blogs table---------------------------------------------
		
		$table_title = "digradio-blogs";
		$class = "b-edit-select";
		echo "<table class='edit-table'>";
		echo "<tr><th colspan=2>$table_title</th></tr>";	   
	   $blog_count = (int)$conf->a['blogs-count-digradio'];
		$ctr=1;
		foreach($h as $x)
		{
			if($x['post_type']=='blog' && $ctr<=$blog_count){
				$str =  'h1'.$ctr;
				echo "<tr><td id=".$ctr.",".$x['id'].">".$x['title'];
				echo "</td>";
		   	
				echo "<td><select class='edit-selection'>";
					foreach($this->all_post as $y)
					{
						if($y['post_type']=='blog')
							echo "<option value=".$y[$column].",".$y['id'].">".$y['title']."</option>";   	
					}
				echo "</select>";
				echo "<span  class='$class edit-select'>select</span>";
				echo "</td></tr>";

			$ctr++;
			}
		}
	if($blog_count>1) 
		echo "<tr><td colspan=2 class='remove-h1 edit-pointer' id=".$table_title."-".$blog_count.">REMOVE</td>";		  
	echo "<tr><td colspan=2 class='add-h1 edit-pointer' id=".$table_title."-".$blog_count.">ADD</td>"; 
	echo "</table>";
	//end table------------------------------------------------------------------
	//make review table---------------------------------------------
		
		$table_title = "digradio-reviews";
		$class = "r-edit-select";
		$review_count = (int)$conf->a['reviews-count-digradio'];
		echo "<table class='edit-table'>";
		echo "<tr><th colspan=2>$table_title</th></tr>";	   
	   
		$ctr=1;
		foreach($h as $x)
		{
			if($x['post_type']=='review' && $ctr<=$review_count){
				$str =  'h1'.$ctr;
				echo "<tr><td id=".$ctr.",".$x['id'].">".$x['title'];
				echo "</td>";
		   	
				echo "<td><select class='edit-selection'>";
					foreach($this->all_post as $y)
					{
						if($y['post_type']=='review')
							echo "<option value=".$y[$column].",".$y['id'].">".$y['title']."</option>";   	
					}
				echo "</select>";
				echo "<span  class='$class edit-select'>select</span>";
				echo "</td></tr>";

			$ctr++;
			}
		}
	if($review_count>1) 
		echo "<tr><td colspan=2 class='remove-h1 edit-pointer' id=".$table_title."-".$review_count.">REMOVE</td>";		  
	echo "<tr><td colspan=2 class='add-h1 edit-pointer' id=".$table_title."-".$review_count.">ADD</td>"; 
	echo "</table>";
	//end table------------------------------------------------------------------
	}
	function render_form($ptype,$e,$o,$id,$cmd)
	{
		
		echo "<form name='f1' action='editor?ptype=$ptype&cmd=edit&id=$id' method='POST'  accept-charset='utf-8' enctype='multipart/form-data'>";
		
		
					
 								   	
		$authors = $this->get_authors();
		p_select('author',$authors,$e,'id','name',$o);
		if($ptype=='review')
		{
			$enum_data=get_enum('content_post','review_type');
			p_select('review_type',$enum_data,$e,'0','1',$o);
			$enum_rating=array(array('rating' => 1),array('rating' => 2),array('rating' => 3),array('rating' => 4),array('rating' => 5),array('rating' => 6),array('rating' => 7),array('rating' => 8),array('rating' => 9),array('rating' => 10));
			p_select('rating',$enum_rating,$e,'rating','rating',$o);		
		}	
		else
		{
			$e['review_type'] = 'n/a';
			p_hidden('review_type',$o,$e);
			$e['rating'] = 0;
			p_hidden('rating',$o,$e);
		}
		p_input('title',$o,$e);
		
		echo "<hr />";
	
		thick_box($e['image'],$e['thumb']);		
		
		p_file('upload_image',$o);
		echo "<hr />";				
		echo "<h3>Content</h3>";
		echo "<textarea name='content' rows=max cols=80>".$e['content']."</textarea>";
		echo "<hr />";				
		echo "<h3>Secondary</h3>"; 
		echo "<textarea name='secondary' rows=max cols=80>".$e['secondary']."</textarea>";
		echo "<hr />";		

		echo "<h2>Meta Options</h2>";
		
		p_input('meta_title',$o,$e);
		p_input('meta_searchtitle',$o,$e);
		p_input('meta_description',$o,$e);
		p_input('meta_keywords',$o,$e);
		p_input('meta_searchdescription',$o,$e);
		
		p_hidden('start',$o,$e);
		p_hidden('end',$o,$e);
		p_hidden('owner',$o,$e);
		p_hidden('category',$o,$e);
		
		p_hidden('access_cost',$o,$e);
		p_hidden('access_expiry',$o,$e);
		p_hidden('access_currency',$o,$e);
		p_hidden('playlist',$o,$e);
		p_hidden('delivery',$o,$e);
		p_hidden('buffer',$o,$e);
		p_hidden('index',$o,$e);
		p_hidden('resize_image',$o,$e);
		p_hidden('post_type',$o,$e);
		p_hidden('resize_x_thumb',$o,$e);
		p_hidden('resize_y_thumb',$o,$e);
		p_hidden('resize_thumb',$o,$e);
		
		if($cmd=='edit')
		{
			$e['id'] = $id;
			p_hidden('id',$o,$e);
		}
		p_submit('save',$id);
		echo "</form>";
		
	}	
	
}

?>
